package my.climbing;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import my.db.*;

public class SelectMountainActivity extends ListActivity{

	ImageView iv;
	ArrayList<String> list=new ArrayList<String>();
	private int mSelectedRow = 0;
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectmountain);
		
		downLoadJSON();
		setListAdapter(new MyListAdapter(this, R.layout.selectmtrow, list));
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
//		super.onListItemClick(l, v, position, id);
//		mSelectedRow = position; //set the selected row
		
		Intent i = new Intent();
		i.setClass(this, SelectPathActivity.class);
		i.putExtra("mountain", list.get(position));
		startActivity(i);
	}
	
	public void downLoadJSON()
	{
		String json="";
		String strUrl="http://211.238.142.88:8080/MapPointProject/map/mountain_list.jsp";
		try
		{
		   URL url=new URL(strUrl);
		   HttpURLConnection conn=(HttpURLConnection)url.openConnection();
		   if(conn!=null)
		   {
			   String str="";
			   BufferedReader in=new BufferedReader(
					   new InputStreamReader(conn.getInputStream()));
			   while(true)
			   {
				   str=in.readLine();
				   if(str==null)
					   break;
				   json+=str;
			   }
			   in.close();
			   
			   JSONArray ja=new JSONArray(json);
			   for(int i=0;i<ja.length();i++)
			   {
				   JSONObject obj=ja.getJSONObject(i);
				   String strMt = obj.getString("mountain");
				   
				   list.add(strMt);
			   }
		   }
		}catch(Exception ex)
		{
		   Toast.makeText(this, ex.getMessage(), 
				   Toast.LENGTH_SHORT).show();	
		}
		
	}
	
}

class MyListAdapter extends BaseAdapter
{

	ArrayList<String> list;
	int layout;
	LayoutInflater in;
	Context mc;
	
	public MyListAdapter(Context c,int lay,ArrayList<String> arr)
	{
		list=arr;
		layout=lay;
		mc=c;
		in=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
	}
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup viewGroup) {
		// TODO Auto-generated method stub
		if(convertView==null)
		{
			convertView=in.inflate(layout, null);
		}
		String mt = list.get(pos);
		TextView tv = (TextView)convertView.findViewById(R.id.list_mt);
		tv.setText(mt);
//		try
//		{
//		   String strUrl="http://211.238.142.65:8080/MovieProject/movie/image/"+vo.getS_poster();
//		   URL url=new URL(strUrl);
//		   
//		   HttpURLConnection conn=(HttpURLConnection)url.openConnection();
//		   if(conn!=null)
//		   {
//			   InputStream is=conn.getInputStream();
//			   BufferedInputStream bi=new BufferedInputStream(is);
//			   Bitmap bm=BitmapFactory.decodeStream(bi);
//			   is.close();
//			   bi.close();
//			   
//			   iv.setImageBitmap(bm);
//		   }
//		}catch(Exception ex){}
		
		return convertView;
	}
	
}
